import threading



tickets = 50
threadLock = threading.Lock()

class ThreadingAsyncTicket(threading.Thread):

    def __init__(self, threadId, threadName, threadDelay):
        threading.Thread.__init__(self)
        self.threadId = threadId
        self.threadName = threadName
        self.threadDelay = threadDelay

    def run(self):
        print("开始线程", self.threadName)
        for i in range(5):
            threadLock.acquire()
            self.sellTicket(self.threadName)
            threadLock.release()

        print("结束线程", self.threadName)


    def sellTicket(self,threadName):
        global tickets
        if tickets == 0:
            self.threadName.exit()
        else:
            tickets -= 1
            print("leave ：" + str(tickets))


items = range(10)
for i in items:
    window = ThreadingAsyncTicket(i, "window-" + str(i), 0.01)
    window.start()
    window.join()
print("主线程结束")
